package com.tuzhi.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class ListTzUtil {

	public static boolean isEmpty(List list) {
		if (list == null || list.isEmpty()) {
			return true;
		}
		return false;
	}

	public static boolean isNotEmpty(List list) {
		return !isEmpty(list);
	}

	public static Integer size(List list) {
		if (isNotEmpty(list)) {
			return list.size();
		}
		return 0;
	}

	/**
	 * 如果为null转new ArrayList<>();
	 * @param list
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> null2Instance(List<T> list) {
		if (list == null) {
			return new ArrayList<>();
		}
		return list;
	}

	/**
	 * 将数组转为可增删的List
	 * @param t
	 * @return
	 * @param <T>
	 */
	public static <T> List<T> toList(T ... t) {
		List<T> list = new ArrayList<>();
		if (t != null) {
			for (T t1 : t) {
				list.add(t1);
			}
		}
		return list;
	}

	/**
	 * 如果为Empty转为null
	 * @param list
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> empty2Null(List<T> list) {
		if (isEmpty(list)) {
			return null;
		}
		return list;
	}

	/**
	 * 根据传入的list，按指定的值为新起点，前面的值往后排的方式，创建新的list
	 * @param src
	 * @param tag
	 * @return
	 */
	public static List<String> restartByTag(List<String> src, String tag) {
		if (src == null || src.size() == 0) {
			return src;
		}
		List<String> rs = new ArrayList<>();
		int num = src.indexOf(tag);
		for (int i = 0; i < src.size(); i++) {
			int tmpNum = i+num;
			if (tmpNum >= src.size()) {
				rs.add(src.get(tmpNum-src.size()));
			} else {
				rs.add(src.get(tmpNum));
			}
		}
		return rs;
	}

	/**
	 * 获取指定玩素后面的所有元素（包含指定的元素）
	 * @param src
	 * @param tag
	 * @return
	 */
	public static List lastListByTag(List src, Object tag) {
		int idx = src.indexOf(tag);
		if (idx == 0) {
			return src;
		}
		List rs = new ArrayList();
		for (int i = idx; i < src.size(); i++) {
			rs.add(src.get(i));
		}
		return rs;
	}

	public static void main(String[] args) {
		List<Integer> list = toList(new Integer[] {1, 2});
		System.out.println(list);
		System.out.println(toList(new BigDecimal[] {BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.TEN}));
	}

}
